package uit.nfcmenu.webservice;

import java.net.URLEncoder;

import android.util.Log;

public class CommonWS {
	private static final String tag = CommonWS.class.getSimpleName();

//	private static final String SERVER_URL 		= "http://203.162.202.51/vsmenu";
	private static final String SERVER_URL 		= "http://203.162.202.55/vsmenu";
	private static final String EQUAL 			= "=";
	private static final String QUERY 			= "?";
	private static final String AND 			= "&";
	private static final String SLASH 			= "/";
	
	/*
	 * 1- Login, logout
	 * http://203.162.202.55/vsmenu/Service.svc/login?username=user&pass=123456&device=10
	 * http://203.162.202.55/vsmenu/Service.svc/logout?user=user&deviceid=10
	 * 
	 * 2- Get Menu
	 * http://203.162.202.55/vsmenu/Service.svc/getmenus?restid=2
	 * 
	 * 3- Get Item
	 * http://203.162.202.55/vsmenu/Service.svc/getitems?restid=2
	 * http://203.162.202.55/vsmenu/Service.svc/getitemsbymenu?menuid=1
	 * 
	 * 4- Get Order, Create Order and Change status of Orders
	 * http://203.162.202.55/vsmenu/Service.svc/getorders?restid=2&version=0
	 * http://203.162.202.55/vsmenu/Service.svc/getordersbystatus?restid=2&status=NEW&version=0
	 * http://203.162.202.55/vsmenu/Service.svc/createorder?user=user&restId=2&itemId=1&status=New&tableNo=1&quantity=10&comment=LanTest1
	 * http://203.162.202.55/vsmenu/Service.svc/changeorder?orderid=8&status=Cooking&comment=LanTest2
	 * 
	 * 5- Get notify
	 * http://203.162.202.55/vsmenu/Service.svc/getnotify?restid=2
	 */
//	public static String webURL 					= "http://vm-tms.appspot.com/";
	private static String serviceURL 				= SERVER_URL + "/Service.svc/";

	private static final String ATTRB_USERNAME 	= "user";
	private static final String ATTRB_REST_ID 	= "restid";
	private static final String ATTRB_DEVICE_ID = "deviceid";
	private static final String ATTRB_MENU_ID 	= "menuid";
	private static final String ATTRB_ITEM_ID 	= "itemid";
	private static final String ATTRB_ORDER_ID 	= "orderid";
	private static final String ATTRB_VERSION 	= "version";

	private static final String SERVICE_GET_NOTIFY 	= "getnotify";

	public static final String POST_CONTENT_TYPE = "application/json";

	// ======================================
	// Services about User
	// ======================================
	/*
	 * 1- Login, logout
	 */
	// http://203.162.202.55/vsmenu/Service.svc/login?username=user&pass=123456&device=10
	// http://203.162.202.55/vsmenu/Service.svc/logout?user=user&deviceid=10
	public static final class User {
		//public static final String LOGIN_LINK 	= "/resources/user/login=";
//		private static final String SERVICE_LOGIN 	= "login";
		private static final String SERVICE_LOGOUT 	= "logout";
//		private static final String ATTRB_PASSWORD 	= "pass";	
//		public static final String SERVICE_JSON_BODY = "{\"username\":\"%s\",\"password\":\"%s\",\"deviceid\":\"%s\"}";
		private static final String SERVICE_POST_LOGIN 	= "postlogin";
		public static final String JSON_USERNAME 		= "username";
		public static final String JSON_PASSWORD 		= "password";
		public static final String JSON_DEVICE_ID 		= "deviceid";

		// http://203.162.202.55/vsmenu/Service.svc/login?username=user&pass=123456&device=10
//		public static String createUrlLogin(String username, String password, String deviceID) {
////			String url = webURL + LOGIN_LINK + username + "," + password + "," + deviceID;
//			//return url;
//			StringBuilder url = new StringBuilder(serviceURL);
//			url.append(SERVICE_LOGIN);		url.append(QUERY);
//			url.append(ATTRB_USERNAME); 	url.append(EQUAL); url.append(username);	url.append(AND);
//			url.append(ATTRB_PASSWORD); 	url.append(EQUAL); url.append(password);	url.append(AND);
//			url.append(ATTRB_DEVICE_ID); 	url.append(EQUAL); url.append(deviceID);
//			
//			Log.i(tag, "createUrlLogin() "+url.toString());
//			return url.toString();
//		}
		
		public static String createUrlPostLogin() {
			return serviceURL + SERVICE_POST_LOGIN;
		}

		// http://203.162.202.55/vsmenu/Service.svc/logout?user=user&deviceid=10
		public static String createUrlLogout(String username, String deviceID) {
			StringBuilder url = new StringBuilder(serviceURL);
			url.append(SERVICE_LOGOUT);		url.append(QUERY);
			url.append(ATTRB_USERNAME); 	url.append(EQUAL); url.append(username);	url.append(AND);
			url.append(ATTRB_DEVICE_ID); 	url.append(EQUAL); url.append(deviceID);
			
			Log.i(tag, "createUrlLogout() "+url.toString());
			return url.toString();	
		}
	}

	// ======================================
	// Services about Menu
	// ======================================
	/* 
	 * 2- Get Menu
	 */
	// http://203.162.202.55/vsmenu/Service.svc/getmenus?restid=2
	public static final class Menu {
		//public static final String GET_MENUS_LINK 	= "/resources/menu/resID/";
		private static final String SERVICE_GET_MENUS 	= "getmenus";

		// http://203.162.202.55/vsmenu/Service.svc/getmenus?restid=2
		public static String createUrlGetMenus(String resID) {
//			String url =  webURL + GET_MENUS_LINK + resID;
//			Log.i(tag, "createUrlGetMenuInServer() "+url);
//			return url;
			StringBuilder url = new StringBuilder(serviceURL);
			url.append(SERVICE_GET_MENUS);	url.append(QUERY);
			url.append(ATTRB_REST_ID); 		url.append(EQUAL); url.append(resID);
			
			Log.i(tag, "createUrlGetMenus() "+url.toString());
			return url.toString();	
		}
	}

	// ======================================
	// Services about Item
	// ======================================
	/* 
	 * 3- Get Item
	 */
	// http://203.162.202.55/vsmenu/Service.svc/getitems?restid=2
	// http://203.162.202.55/vsmenu/Service.svc/getitemsbymenu?menuid=1
	public static final class Item {
		//public static final String GET_ITEMS_LINK 		= "/resources/item/resID/";
		private static final String SERVICE_GET_ITEMS 			= "getitems";
		private static final String SERVICE_GET_ITEMS_BY_MENU 	= "getitemsbymenu";
		
		// http://203.162.202.55/vsmenu/Service.svc/getitems?restid=2
		public static String createUrlGetItems(String resID) {
//			String url = webURL + GET_ITEMS_LINK + resID;
//			Log.i(tag, "createUrlGetItems() "+url);
//			return url;
			StringBuilder url = new StringBuilder(serviceURL);
			url.append(SERVICE_GET_ITEMS);		url.append(QUERY);
			url.append(ATTRB_REST_ID); 			url.append(EQUAL); url.append(resID);
			
			Log.i(tag, "createUrlGetItems() "+url.toString());
			return url.toString();	
		}
		
		// http://203.162.202.55/vsmenu/Service.svc/getitemsbymenu?menuid=1
		public static String createUrlGetItemsByMenu(String menuId) {
//			String url = webURL + GET_ITEMS_BY_MENU_LINK + menuID;
//			Log.i(tag, "createUrlGetItemsByMenu() "+url);
//			return url;
			StringBuilder url = new StringBuilder(serviceURL);
			url.append(SERVICE_GET_ITEMS_BY_MENU);	url.append(QUERY);
			url.append(ATTRB_MENU_ID); 				url.append(EQUAL); url.append(menuId);
			
			Log.i(tag, "createUrlGetItems() "+url.toString());
			return url.toString();	
		}
	}

	// ======================================
	// Services about Order
	// ======================================
	/* 
	 * 4- Get Order, Create Order and Change status of Orders // TODO get data by version
	 */
	// http://203.162.202.55/vsmenu/Service.svc/getorders?restid=2&version=0
	// http://203.162.202.55/vsmenu/Service.svc/getordersbystatus?restid=2&status=NEW&version=0
	// http://203.162.202.55/vsmenu/Service.svc/createorder?user=user&restId=2&itemId=1
	// 		&status=New&tableNo=1&quantity=10&comment=LanTest1
	// http://203.162.202.55/vsmenu/Service.svc/changeorder?orderid=8&status=Cooking&comment=LanTest2
	public static final class Order {
//		public static final String GET_ORDERS_LINK 			= "/resources/order/restID=";
//		public static final String POST_ORDER_LINK 			= "/resources/order/";
//		public static final String POST_CHANGE_ORDER_STATUS = "/status/status=";
//		public static final String PRE_CHANGE_ORDER_STATUS 	= "/resources/order/";
//		public static final String REST_ID 					= "restId";
//		public static final String ITEM_ID 					= "itemId";
//		public static final String STATUS 					= "status";
//		public static final String TABLE_NUM 				= "tableNo";
//		public static final String QUANTITY 				= "quantity";
//		public static final String COMMENT 					= "comment";
		private static final String SERVICE_GET_ORDERS 			= "getorders";
		private static final String SERVICE_CREATE_ORDERS 		= "createorder";
		private static final String SERVICE_CHANGE_ORDER_STATUS = "changeorder";
		
		private static final String ATTRB_STATUS 		= "status";
		private static final String ATTRB_TABLE_NO 		= "tableNo";
		private static final String ATTRB_QUANTITY	 	= "quantity";
		private static final String ATTRB_COMMENT 		= "comment";

		// http://203.162.202.55/vsmenu/Service.svc/getorders?restid=2&version=0
		public static String createUrlGetOrders(String resId, int version) {
//			String url = webURL + GET_ORDER_LIST_LINK + resID;
//			Log.i(tag, "createUrlGetOrders() "+url);
//			return url;
			StringBuilder url = new StringBuilder(serviceURL);
			url.append(SERVICE_GET_ORDERS);	url.append(QUERY);
			url.append(ATTRB_REST_ID); 		url.append(EQUAL); url.append(resId);	url.append(AND);
			url.append(ATTRB_VERSION); 		url.append(EQUAL); url.append(version);
			
			Log.i(tag, "createUrlLogin() "+url.toString());
			return url.toString();
		}

		// TODO need implement or not ???
		// http://203.162.202.55/vsmenu/Service.svc/getordersbystatus?restid=2&status=NEW&version=0
		

		// http://203.162.202.55/vsmenu/Service.svc/createorder?user=user&restId=2
		// 		&itemId=1&status=New&tableNo=1&quantity=10&comment=LanTest1
		public static String createUrlPostOrder(String resId, String user, 
				int itemId, String status, int tableNo, int quantity, String comment) {
//			comment = URLEncoder.encode(comment + " ");
//			String url = webURL + POST_ORDER_LINK;
//			url += REST_ID + EQUAL + resID;
//			url += AND + ITEM_ID + EQUAL + itemid;
//			url += AND + STATUS + EQUAL + status;
//			url += AND + TABLE_NUM + EQUAL + tableNo;
//			url += AND + QUANTITY + EQUAL + quantity;
//			url += AND + COMMENT + EQUAL + comment;
//			return url;
			StringBuilder url = new StringBuilder(serviceURL);
			url.append(SERVICE_CREATE_ORDERS);	url.append(QUERY);
			url.append(ATTRB_USERNAME); 		url.append(EQUAL); url.append(user);	url.append(AND);
			url.append(ATTRB_REST_ID); 			url.append(EQUAL); url.append(resId);	url.append(AND);
			url.append(ATTRB_ITEM_ID); 			url.append(EQUAL); url.append(itemId); 	url.append(AND);
			url.append(ATTRB_STATUS); 			url.append(EQUAL); url.append(status);	url.append(AND);
			url.append(ATTRB_TABLE_NO); 		url.append(EQUAL); url.append(tableNo);	url.append(AND);
			url.append(ATTRB_QUANTITY); 		url.append(EQUAL); url.append(quantity);url.append(AND);
			url.append(ATTRB_COMMENT); 			url.append(EQUAL); url.append(comment);	
			
			Log.i(tag, "createUrlPostOrder() "+url.toString());
			return url.toString();
		}

		// http://203.162.202.55/vsmenu/Service.svc/changeorder?orderid=8&status=Cooking&comment=LanTest2
		public static String createUrlChangeOrderStatus(int orderId, String status, String comment) {
			if (comment == null || "".equals(comment)) {
				comment = " ";
			}
			comment = URLEncoder.encode(comment);
//			return webURL + PRE_CHANGE_ORDER_STATUS + orderId
//					+ POST_CHANGE_ORDER_STATUS + status + AND + COMMENT
//					+ EQUAL + URLEncoder.encode(comment);
			StringBuilder url = new StringBuilder(serviceURL);
			url.append(SERVICE_CHANGE_ORDER_STATUS);	url.append(QUERY);
			url.append(ATTRB_ORDER_ID); 		url.append(EQUAL); url.append(orderId);	url.append(AND);
			url.append(ATTRB_STATUS); 			url.append(EQUAL); url.append(status);	url.append(AND);
			url.append(ATTRB_COMMENT); 			url.append(EQUAL); url.append(comment);	
			
			Log.i(tag, "createUrlPostOrder() "+url.toString());
			return url.toString();
		}
	}

	 /* 
	  * 5- Get notify
	  */
	// http://203.162.202.55/vsmenu/Service.svc/getnotify?restid=2
	public static String createUrlGetNotify(String resId) {
//				String url = webURL + GET_ORDER_LIST_LINK + resID;
//				Log.i(tag, "createUrlGetOrders() "+url);
//				return url;
		StringBuilder url = new StringBuilder(serviceURL);
		url.append(SERVICE_GET_NOTIFY);	url.append(QUERY);
		url.append(ATTRB_REST_ID); 		url.append(EQUAL); url.append(resId);
		
		Log.i(tag, "createUrlGetNotify() "+url.toString());
		return url.toString();
	}

	public static String getHttpUrl(String fileURL) {
//		try {
//			fileURL = URLEncoder.encode(fileURL, "UTF-8");
//		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
			fileURL = fileURL.replace(" ", "%20");
//		}
		//Log.i(tag, "fileURL="+fileURL);
		return fileURL;
	}

	
	public static String getLinkIfLack(String img) {
		if (img.startsWith(SLASH)) {
			img = CommonWS.SERVER_URL + img;
		}
		return img;
	}
}
